package tc.alert.tckejisoc.kafka.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
import tc.alert.tckejisoc.domain.EventBase;
import tc.alert.tckejisoc.dto.EdrDTO;
import tc.alert.tckejisoc.rule.LogRuleManager;
import tc.alert.tckejisoc.wx.WeChatRobot;

@Service
@Slf4j
public class KafkaConsumerService {

    @Autowired
    private LogRuleManager logRuleManager;
    @Autowired
    private KafkaProducerService producerService;

    /**
     * 监听指定 Topic 的消息
     *
     * @param message 接收到的消息内容
     */
//    @KafkaListener(topics = "edr-event-alert", groupId = "my-group")
    public void listenEdrMessage(String message) throws Exception {
//        ack.acknowledge(); // 手动提交
        EdrDTO edrDTO = JSON.parseObject(message, EdrDTO.class);
        log.debug("推送消息" + edrDTO.toString());
        WeChatRobot.sendEdrMessage(edrDTO);
    }

//    @KafkaListener(topics = "waf-event-alert", groupId = "my-group")
    public void listenWafMessage(String message) throws Exception {
//        ack.acknowledge(); // 手动提交
        EdrDTO wafDTO = JSON.parseObject(message, EdrDTO.class);
        log.debug("推送消息" + wafDTO.toString());
        WeChatRobot.sendWafMessage(wafDTO);
    }

    //    @KafkaListener(topics = "ty-event-alert", groupId = "my-group")
    public void listenTyMessage(String message) throws Exception {
//        ack.acknowledge(); // 手动提交
        EdrDTO edrDTO = JSON.parseObject(message, EdrDTO.class);
        log.debug("推送消息" + edrDTO.toString());
        WeChatRobot.sendEdrMessage(edrDTO);
    }

//    @KafkaListener(topics = "hr-event-alert", groupId = "my-group")
    public void listenHrMessage(String message) throws Exception {
//        ack.acknowledge(); // 手动提交
        EdrDTO edrDTO = JSON.parseObject(message, EdrDTO.class);
        log.debug("推送消息" + edrDTO.toString());
        WeChatRobot.sendHrMessage(edrDTO);
    }

    //    @KafkaListener(topics = "waf-banned", groupId = "my-group")
    public void listenWafbdMessage(String message) throws Exception {
//        ack.acknowledge(); // 手动提交
        EdrDTO wafDTO = JSON.parseObject(message, EdrDTO.class);

        log.debug("封禁IP" + wafDTO.toString());

    }

    //    @KafkaListener(topics = "edr-banned", groupId = "my-group")
    public void listenEdrBdMessage(String message) throws Exception {
//        ack.acknowledge(); // 手动提交
        EdrDTO wafDTO = JSON.parseObject(message, EdrDTO.class);
        log.debug("封禁IP" + wafDTO.toString());

    }

    //    @KafkaListener(topics = "ty-banned", groupId = "my-group")
    public void listenTyBdMessage(String message) throws Exception {
//        ack.acknowledge(); // 手动提交
        EdrDTO wafDTO = JSON.parseObject(message, EdrDTO.class);
        log.debug("封禁IP" + wafDTO.toString());

    }

    //    @KafkaListener(topics = "topics-ty-log", groupId = "my-group")
    public void listenKafakLog(String message) throws Exception {
//        ack.acknowledge(); // 手动提交

        JSONObject jsonObject = JSONObject.parseObject(message);
        String str = jsonObject.getString("log-name");
        str = "hr01";
        EventBase unifiedLog = logRuleManager.parseLog(message, str);

    }


}